﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DatabaseModule;

namespace EMC
{
   public class FileManagement
    {
        private DAO dbConn;

        private databaseConfiguration config;

        public FileManagement(databaseConfiguration dbConfig)
        {
            this.config = dbConfig;
            dbConn = dbFactory.newDB(config.dbType, config.dbConnectionString);
            //dbConn.SetVerboseMode(true);
        }

        public bool addfile(string file_name, string description, string userid)
        {
            bool result = false;
            KeyValues fieldVals = new KeyValues();
            fieldVals.Add(config.tableFiles_UserID, userid);
            fieldVals.Add(config.tableFiles_Filename, file_name);
            fieldVals.Add(config.tableFiles_Description, description);
            result = dbConn.InsertRow("files", fieldVals);
            Console.WriteLine(result);
            return result;
        }

        // Assuming that for unentered fields the an empty string is passed
        public bool editFile(string file_name, string description, string userid, string locale)
        {

            KeyValues fieldVals = new KeyValues();
            KeyValues keyVals = new KeyValues();
            string empty = "";

            if (description != empty)
            {

                keyVals.Add(config.tableFiles_Description, description);

            }
            if (userid != empty)
            {

                keyVals.Add(config.tableFiles_UserID, userid);

            }

            fieldVals.Add(config.tableFiles_Filename, file_name);
            return dbConn.UpdateRows(config.tableFiles, keyVals, fieldVals);

        }
       
       public bool removeFile(string userid,string filename)
        {
            KeyValues compositeConstraint = new KeyValues();
            compositeConstraint.Add(config.tableFiles_UserID, userid);
            compositeConstraint.Add(config.tableFiles_Filename, filename);
            string fileid = dbConn.GetValue(config.tableFiles, config.tableFiles_Fileid, compositeConstraint);
            return removeFile(fileid);
        }

        private bool removeFile(string fileid)
        {
            KeyValues keyVals = new KeyValues();
            keyVals.Add(config.tableFiles_Fileid, fileid);
            return dbConn.DeleteRows(config.tableFiles, keyVals);
        }

        public List<string> userFiles(string username)
        {
            KeyValues keyVals = new KeyValues();
            keyVals.Add(config.tableUsers_ID, username);
            
            List<Dictionary<string, string>> userfileList = dbConn.GetRows(config.tableFiles, new string[] { config.tableFiles_Filename }, keyVals);

            List<string> simplifiedList = userfileList.ConsolidateValues(config.tableFiles_Filename);

            return simplifiedList;           
        }
    }
}